package com.gsy.controller.student;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsy.aop.annotation.ParameterValid;
import com.gsy.aop.annotation.ResubmitData;
import com.gsy.aop.annotation.TokenValid;
import com.gsy.entity.HealthEntity;
import com.gsy.enumerate.SystemStatusEnum;
import com.gsy.request.AddHealthRequest;
import com.gsy.request.GetUsersByTypeAndCharRequest;
import com.gsy.response.BaseResponse;
import com.gsy.service.HealthService;
import com.gsy.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 健康填报表 前端控制器
 * </p>
 *
 * @author gsy
 * @since 2022-03-03
 */
@RestController
@RequestMapping(value = "students", consumes = "application/json", produces = "application/json")
@Api(value = "健康填报表controller", tags = {"tb_health 操作接口"})
public class HealthController {

    private final static Logger LOGGER = LoggerFactory.getLogger(HealthController.class);

    @Resource
    private HealthService healthService;

    /**
    * 新增健康填报表
    * @param request
    * @return
    */
    @ApiOperation(value = "健康填报表", notes = "健康填报表")
    @PostMapping("sethealth")
    @ParameterValid(apiName = "健康填报表", formatObj = AddHealthRequest.class)
    @TokenValid
    @ResubmitData//防止重复提交
    public BaseResponse addHealth(@RequestBody AddHealthRequest request) {

        int count = healthService.count(Wrappers.lambdaQuery(new HealthEntity())
                .eq(HealthEntity::getUId, request.getuId())
                .like(HealthEntity::getCreatetime, DateUtil.getStringDateShort()));
        if (count != 0){
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "今日已经填写过了！");
        }

        HealthEntity entity = new HealthEntity();
        BeanUtils.copyProperties(request, entity);

        boolean flag = healthService.save(entity);
        if (flag) {
            return new BaseResponse(SystemStatusEnum.SUCCESS);
        } else {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "新增失败！");
        }
    }


    /**
    * 获取当天所有填报表
    * @return
    */
    @ApiOperation(value = "获取当天所有填报表", notes = "获取当天所有填报表")
    @GetMapping("gethealthNowDay")
    @TokenValid
    public BaseResponse getHealthList() {

        List<HealthEntity> entities = healthService.list(Wrappers.lambdaQuery(new HealthEntity()).like(HealthEntity::getCreatetime,
                DateUtil.getStringDateShort()).orderByDesc(HealthEntity::getHId));

        if (CollectionUtil.isNotEmpty(entities)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
    }
    /**
    * 获取当月所有填报表
    * @return
    */
    @ApiOperation(value = "获取当月所有填报表", notes = "获取当月所有填报表")
    @GetMapping("gethealthNowMonth")
    @TokenValid
    public BaseResponse getHealthNowMonth() {

        String thisMonth = DateFormatUtils.format(new Date(), "yyyy-MM");
        List<HealthEntity> entities = healthService.list(Wrappers.lambdaQuery(new HealthEntity()).like(HealthEntity::getCreatetime,
                thisMonth));

        if (CollectionUtil.isNotEmpty(entities)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
    }

    /**
    * 获取当天某用户报表
    * @return
    */
    @ApiOperation(value = "获取当天某用户报表", notes = "获取当天某用户报表")
    @GetMapping("getHealthNowDayByid/{uId}")
    @TokenValid
    public BaseResponse getHealthNowDayById(@PathVariable Integer uId) {

        List<HealthEntity> entities = healthService.list(Wrappers.lambdaQuery(new HealthEntity()).like(HealthEntity::getCreatetime,
                DateUtil.getStringDateShort())
        .eq(HealthEntity::getUId,uId));

        if (CollectionUtil.isNotEmpty(entities)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
    }

    /**
    * 获取所有报表
    * @return
    */
    @ApiOperation(value = "获取所有报表", notes = "获取所有报表")
    @GetMapping("getAllHealth")
    @TokenValid
    public BaseResponse getAllHealth(@RequestParam(value = "userId", required = false) Long userId) {

        LambdaQueryWrapper<HealthEntity> wrapper = Wrappers.lambdaQuery(HealthEntity.class).eq(userId != null, HealthEntity::getUId, userId);
        List<HealthEntity> entities = healthService.list(wrapper);

        if (CollectionUtil.isNotEmpty(entities)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
    }
    /**
    * 获取当天所有填报表与总数量(分页获取)
    * @return
    */
    @ApiOperation(value = "获取当天所有填报表与总数量(分页获取)", notes = "获取当天所有填报表与总数量(分页获取)")
    @GetMapping("gethealthNowDayPage/{currPage}/{pageNum}")
    @TokenValid
    public BaseResponse gethealthNowDayPage(@PathVariable Integer pageNum,@PathVariable Integer currPage) {

        Page<HealthEntity> page = new Page<>(currPage, pageNum);
        Page<HealthEntity> entities = healthService.page(page,
                Wrappers.lambdaQuery(new HealthEntity()).like(HealthEntity::getCreatetime,
                        DateUtil.getStringDateShort()));
        List<HealthEntity> records = entities.getRecords();

        if (CollectionUtil.isNotEmpty(records)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            success.setTotal(page.getTotal());
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
     }
}
